/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.facedoor.modular.idcardhistory.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.util.IpAddressUtil;
import vip.xiaonuo.core.util.PoiUtil;
import vip.xiaonuo.facedoor.modular.idcardhistory.entity.IdCardHistory;
import vip.xiaonuo.facedoor.modular.idcardhistory.enums.IdCardHistoryExceptionEnum;
import vip.xiaonuo.facedoor.modular.idcardhistory.param.IdCardHistoryParam;
import vip.xiaonuo.facedoor.modular.idcardhistory.mapper.IdCardHistoryMapper;
import vip.xiaonuo.facedoor.modular.idcardhistory.service.IdCardHistoryService;

import java.util.List;

/**
 * 人证通行记录service接口实现类
 *
 * @author 胡鹏达
 * @date 2022-07-20 14:07:18
 */
@Service
public class IdCardHistoryServiceImpl extends ServiceImpl<IdCardHistoryMapper, IdCardHistory>
        implements IdCardHistoryService {

    @Override
    public PageResult<IdCardHistory> page(IdCardHistoryParam idCardHistoryParam) {
        QueryWrapper<IdCardHistory> queryWrapper = getListWrapper(idCardHistoryParam);
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    private static QueryWrapper<IdCardHistory> getListWrapper(IdCardHistoryParam idCardHistoryParam) {
        QueryWrapper<IdCardHistory> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(idCardHistoryParam)) {

            // 根据通行人员姓名 查询
            if (ObjectUtil.isNotEmpty(idCardHistoryParam.getName())) {
                queryWrapper.lambda().like(IdCardHistory::getName, idCardHistoryParam.getName());
            }
            // 根据身份证号 查询
            if (ObjectUtil.isNotEmpty(idCardHistoryParam.getIdCard())) {
                queryWrapper.lambda().like(IdCardHistory::getIdCard, idCardHistoryParam.getIdCard());
            }
            // 根据健康码 1:绿,2:黄,3:红 查询
            if (ObjectUtil.isNotEmpty(idCardHistoryParam.getHealthColor())) {
                queryWrapper.lambda().eq(IdCardHistory::getHealthColor, idCardHistoryParam.getHealthColor());
            }
            // 根据行程结果 1:低风险 2:中高风险 3:无行程数据 查询
            if (ObjectUtil.isNotEmpty(idCardHistoryParam.getXcLevel())) {
                queryWrapper.lambda().eq(IdCardHistory::getXcLevel, idCardHistoryParam.getXcLevel());
            }
            if (ObjectUtil.isNotEmpty(idCardHistoryParam.getDeviceName())){
                queryWrapper.lambda().like(IdCardHistory::getDeviceName,idCardHistoryParam.getDeviceName());
            }
            // 通行时间查询
            if (idCardHistoryParam.getComparedStartDate() != null && idCardHistoryParam.getComparedEndDate() != null) {
                queryWrapper
                        .lambda()
                        .between(
                                IdCardHistory::getComparedDate,
                                idCardHistoryParam.getComparedStartDate(),
                                idCardHistoryParam.getComparedEndDate());
            }
            queryWrapper.orderByDesc("create_time");
        }
        return queryWrapper;
    }

    @Override
    public List<IdCardHistory> list(IdCardHistoryParam idCardHistoryParam) {
        QueryWrapper<IdCardHistory> listWrapper = getListWrapper(idCardHistoryParam);
        return this.list(listWrapper);
    }

    @Override
    public void add(IdCardHistoryParam idCardHistoryParam) {
        IdCardHistory idCardHistory = new IdCardHistory();
        BeanUtil.copyProperties(idCardHistoryParam, idCardHistory);
        this.save(idCardHistory);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(IdCardHistoryParam idCardHistoryParam) {
        this.removeById(idCardHistoryParam.getId());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(IdCardHistoryParam idCardHistoryParam) {
        IdCardHistory idCardHistory = this.queryIdCardHistory(idCardHistoryParam);
        BeanUtil.copyProperties(idCardHistoryParam, idCardHistory);
        this.updateById(idCardHistory);
    }

    @Override
    public IdCardHistory detail(IdCardHistoryParam idCardHistoryParam) {
        return this.queryIdCardHistory(idCardHistoryParam);
    }

    @Override
    public void export(IdCardHistoryParam idCardHistoryParam) {
        List<IdCardHistory> idCardHistories = this.list(idCardHistoryParam);
        idCardHistories.forEach(idCardHistory -> {
            if (StringUtils.isNotEmpty(idCardHistory.getIdCardImg())) {
                idCardHistory.setIdCardImg(IpAddressUtil.getImageAddIp(idCardHistory.getIdCardImg()));
            }
            if (StringUtils.isNotEmpty(idCardHistory.getImage())) {
                idCardHistory.setImage(IpAddressUtil.getImageAddIp(idCardHistory.getImage()));
            }
        });
        PoiUtil.exportExcelWithStream("人证通行记录.xls", IdCardHistory.class, idCardHistories);
    }

    /**
     * 获取人证通行记录
     *
     * @author 胡鹏达
     * @date 2022-07-20 14:07:18
     */
    private IdCardHistory queryIdCardHistory(IdCardHistoryParam idCardHistoryParam) {
        IdCardHistory idCardHistory = this.getById(idCardHistoryParam.getId());
        if (ObjectUtil.isNull(idCardHistory)) {
            throw new ServiceException(IdCardHistoryExceptionEnum.NOT_EXIST);
        }
        return idCardHistory;
    }
}
